package com.aube.base.goods;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

import com.huyn.baseframework.dynamicload.video.model.GoodsPoint;
import com.huyn.baseframework.dynamicload.video.model.GoodsRoute;
import com.huyn.baseframework.utils.Utils;

import java.util.HashMap;
import java.util.List;

/**
 * Created by hill on 10/24.
 */
public class GoodsHotpotView extends View {
    private String TAG = "Aube/GoodsHotpotView";

    private Paint mPaint;
    private Paint mRipplePaint;

    private int solidColor;
    private int rippleColor;

    private int ALPHA = 102;

    private HashMap<Integer, GoodsRoute> mRoutes;
    private IProgressListener mProgressListener;
    private int mIndexX=0;
    private int mIndexY=0;

    private int mCircleWidth = 200;

    private float counter = 0;

    private OnHotpotClickListener mClickListener;
    private long mLastOnDownTime = 0;

    private boolean mScaleIn = true;
    private float mRadiusRate = 0f;

    private float[] mCurrentXY;

    public GoodsHotpotView(Context context) {
        this(context, null);
    }
    public GoodsHotpotView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }
    public GoodsHotpotView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        solidColor = Color.argb(ALPHA, 255, 255, 255);
        rippleColor = Color.argb(ALPHA, 255, 255, 255);

        mPaint = new Paint();
        mPaint.setColor(solidColor);
        mPaint.setAntiAlias(true);

        mRipplePaint = new Paint();
        mRipplePaint.setColor(rippleColor);
        mRipplePaint.setAntiAlias(true);
    }

    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        if(mClickListener == null)
            return super.onTouchEvent(ev);

        boolean executeEvent = false;

        if(mIndexX == 0 || mIndexY == 0)
            return false;

        float y = ev.getY();
        float x = ev.getX();

        Utils.Log(TAG, "[onTouchEvent] x = " + x + ", y = " + y + ", mIndexX = " + mIndexX + ", mIndexY = " + mIndexY + ", mCircleWidth = " + mCircleWidth);

        if(x >= mIndexX-mCircleWidth/2 && x <= mIndexX+mCircleWidth/2 && y>= mIndexY-mCircleWidth/2 && y <= mIndexY+mCircleWidth/2) {
            Utils.Log(TAG, "[onTouchEvent] x = " + x + ", mIndex = " + mIndexX + ", mIndexY = " + mIndexY + ", mCircleWidth = " + mCircleWidth);
            long current = 0;
            executeEvent = true;
            switch (ev.getAction()) {
                case MotionEvent.ACTION_DOWN:
                    mLastOnDownTime = System.currentTimeMillis();
                    break;
                case MotionEvent.ACTION_UP:
                    current = System.currentTimeMillis();
                    if(current - mLastOnDownTime > 500)
                        return false;
                    mClickListener.onClick(mIndexX, mIndexY);
                    break;
                case MotionEvent.ACTION_MOVE:
                    current = System.currentTimeMillis();
                    if(current - mLastOnDownTime > 500)
                        return false;
                    break;
            }
        }
        return executeEvent;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        if(mProgressListener == null || mRoutes == null || mRoutes.size() == 0) {
            invalidate();
            return;
        }

        int currentProgress = mProgressListener.getCurrentProgress();
        float[] xy = getXY(currentProgress);
        if(xy == null) {
            invalidate();
            return;
        }

        int width = getWidth();
        int height = getHeight();

        mIndexX = (int) (width*xy[0]);
        mIndexY = (int) (height*xy[1]);

        if(mIndexX == 0 || mIndexY == 0) {
            invalidate();
            return;
        }

        canvas.drawCircle(mIndexX, mIndexY, mCircleWidth/4*mRadiusRate/10, mPaint);

        float size = counter%(mCircleWidth/4) + mCircleWidth/4;
        mRipplePaint.setAlpha((int) ((1-size*1f/(mCircleWidth/2))*ALPHA));
        canvas.drawCircle(mIndexX, mIndexY, size*mRadiusRate/10, mRipplePaint);
        counter+=0.75;

        if(mRadiusRate <= 10 && mRadiusRate >= 0) {
            if (mScaleIn)
                mRadiusRate+=1;
            else
                mRadiusRate-=1;
            if(mRadiusRate > 10) mRadiusRate = 10;
            if(mRadiusRate < 0) mRadiusRate = 0;
        }

        if(mRadiusRate == 0 && !mScaleIn && getVisibility() == View.VISIBLE)
            super.setVisibility(GONE);

        invalidate();
    }

    public void setProgressListener(IProgressListener listener, List<GoodsPoint> pointList) {
        mProgressListener = listener;
        if(pointList == null)
            return;
        mRoutes = new HashMap<>();
        for(GoodsPoint point : pointList) {
            if(point.circleTrajectoryList != null) {
                for(GoodsRoute route : point.circleTrajectoryList) {
                    mRoutes.put(route.s, route);
                }
            }
        }
    }

    public void setOnHotpotClickListener(OnHotpotClickListener listener) {
        this.mClickListener = listener;
    }

    private float[] getXY(int currentProgress) {
        mCurrentXY = new float[2];
        try {
            int second = currentProgress/1000;
            GoodsRoute start = mRoutes.get(second);
            GoodsRoute end = mRoutes.get(second + 1);
            if(start != null) {
                if(end == null) {
                    return null;
                } else {
                    float fraction = currentProgress % 1000 * 1f / 1000;
                    mCurrentXY[0] = (end.X - start.X) * fraction + start.X;
                    mCurrentXY[1] = (end.Y - start.Y) * fraction + start.Y;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return mCurrentXY;
    }

    public int[] getCoordinate() {
        int[] coordinate = new int[2];

        coordinate[0] = (int) (getWidth() * mCurrentXY[0] - mCircleWidth/4*mRadiusRate/10);
        coordinate[1] = (int) (getHeight() * mCurrentXY[1] - mCircleWidth/4*mRadiusRate/10);

        return coordinate;
    }

    public int getRadius() {
        return (int) (mCircleWidth/4*mRadiusRate/10);
    }

    public void setVisibility(int state) {
        if(state == View.VISIBLE) {
            super.setVisibility(state);
            mScaleIn = true;
        } else {
            mScaleIn = false;
        }
    }

    public interface IProgressListener {
        public int getCurrentProgress();
    }

    public interface OnHotpotClickListener {
        public void onClick(int x, int y);
    }

}
